package com.base.service;

import com.base.util.QueryUtil;
import com.lazyzhao.util.pagination.Pagination;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppsService extends BasicService
{
  public static final String TABLENAME_APPS = "apps";
  public static final String TABLENAME_USER_BY_APPId = "";

  public int insert(Connection conn, String[] sql)
  {
    int count = 0;
    int[] counts = (int[])null;
    Statement st = null;
    if (sql != null)
      try {
        st = conn.createStatement();
        for (String tempSql : sql) {
          st.addBatch(tempSql);
        }
        counts = st.executeBatch();
        for (int i : counts)
          count += i;
      }
      catch (Exception ex) {
        ex.printStackTrace();
        try
        {
          st.close(); } catch (Exception localException1) {
        } } finally { try { st.close();
        }
        catch (Exception localException2)
        {
        }
      }
    return count;
  }
  public List<Map<String, Object>> findUserToApp(Connection conn, String appId, int exit) throws SQLException {
    String sql = "select username,name,user_company from  users where username in (select username from t_user_app where id='" + appId + "')";
    if (exit == 1) {
      sql = "select username,name,user_company from  users where username  not in (select username from t_user_app where id='" + appId + "') and username!='admin' ";
    }
    return queryMapListBySql(conn, sql, null);
  }
  public Pagination<List<Map<String, Object>>> findApps(Connection conn, Map<String, Object> params, Pagination<List<Map<String, Object>>> pagination) throws SQLException {
    return query(conn, tname("apps"), params, pagination);
  }

  public List<Map<String, Object>> findApps(Connection conn, Map<String, Object> params, int[] startend) throws SQLException {
    int start = 0;
    int end = 999999999;

    if (startend.length > 0) {
      start = startend[0];
    }
    if (startend.length > 1) {
      end = startend[1];
    }

    return queryMapList(conn, tname("apps"), params, start, end);
  }

  public Map<String, Object> findAppsById(Connection conn, String id) throws SQLException {
    HashMap params = new HashMap();
    params.put("id", id);
    return queryMap(conn, tname("apps"), params);
  }

  public int insertApps(Connection conn, Map<String, Object> objects) throws SQLException {
    return this.query.insert(conn, tname("apps"), objects);
  }

  public int updateApps(Connection conn, Map<String, Object> objects, String[] wheres) throws SQLException {
    return this.query.update(conn, tname("apps"), objects, wheres);
  }
  public int updateApps(Connection conn, List<Map<String, Object>> objects, String[] wheres) throws SQLException {
    return this.query.update(conn, tname("apps"), objects, wheres);
  }

  public int deleteApps(Connection conn, String[] ids) throws SQLException {
    Object[][] objs = new Object[ids.length][];
    for (int i = 0; i < ids.length; i++) {
      objs[i] = new Object[]{ ids[i] };
    }
    int[] ints = this.query.batch(conn, "delete from " + tname("apps") + " where id=?", objs);
    int count = 0;
    for (int i : ints) {
      count += i;
    }
    return count;
  }
}